Window = {};
local parent = Widget;

for k,v in pairs(parent)do
    Window[k] = v;
end


Window.__index = Window;

local __call = function()
    local w = parent();
    setmetatable(w, Window);
    w.window = nil;
    w.name = "llui";
    w.x = 0;
    w.y = 0;
    w.windowX = 100;
    w.windowY = 100;
    w.width = 400;
    w.height = 600;
    w.isWindow = true;
    w.backgroundcolor = 0xffeeeeee;
    w.lowRender = 1;
    application.addWidget(w);
    return w;
end

setmetatable(Window,{__call = __call});

if(ui:isPlatform("SDL2"))then
--SDL2-------------------------------
function Window.SetTitle(w,s)
    w.title = s;
    if(w.platformData)then
        w.platformData.setTitle(w,s);
    end
end

function Window.show(self)
    --if(ui:isPlatform("SDL2"))then
        if(self.window==nil)then
            local flag = SDL_WINDOW_OPENGL|SDL_WINDOW_MAXIMIZED|SDL_WINDOW_RESIZABLE;
            self.window = SDL_CreateWindow(self.title,self.windowX,self.windowY,self.width,self.height,flag);
            self.platformData = {};
            self.platformData.name = "SDL2";
            self.platformData.renderer = SDL_CreateRenderer(self.window,2,SDL_RENDERER_ACCELERATED);
            self.platformData.window = self.window; 
            self.platformData.show = function(w) SDL_ShowWindow(w); end;
            self.platformData.hide = function(w) SDL_HideWindow(w); end;
            self.platformData.setTitle = function(w,s)
                SDL_SetWindowTitle(w.platformData.window,s);
            end
            self.quit = function(w)
                SDL_DestroyWindow(w.platformData.window);
                SDL_DestroyRenderer(w.platformData.renderer);
            end

            --SDL_SetRenderDrawColor(self.platformData.renderer,0xee,0xee,0xee,0xee,0xff);
            --SDL_RenderClear(self.platformData.renderer);
            --SDL_RenderPresent(self.platformData.renderer);
        else
            self.platformData.show(self.platformData.window);
        end
    --end
end

function Window.render(self)
    setWindow(self.platformData.window);
    local renderer = setRenderer(self.platformData.renderer);
    self.width,self.height = getWindowSize(self.platformData.window);
    --SDL_RenderClear(self.platformData.renderer);
    local ret = parent.render(self);
    --drawRect(0,0,self.width,self.height,0xff0000ff,true);
    --renderPresent();
    setRenderer(renderer);
    return ret;
end

end

